﻿using Simple.Tools.Verify;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace Simple.Tools.Entity.VerifyAttribute
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	/// <summary>
	/// 验证字符串长度
	/// 数字类型大小范围
	/// </summary>
	public class CusSizeAttribute : BaseVerifyAttribute
	{
		private BaseCheckType _checkType;
		private int _min;
		private int _max;
		/// <summary>
		/// 属性名称必须填写
		/// </summary>
		/// <param name="name"></param>
		/// <param name="min">最小值</param>
		/// <param name="max">最大值</param>
		public CusSizeAttribute(string name, int min=-1, int max=100) : base(name)
		{
			this._errorInfo = "不能为空!";
			this._max = max;
			this._min = min;
			_checkType = SimpleInitialize.GetCheckType();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="o"></param>
		/// <param name="prop"></param>
		/// <returns></returns>
		public override Tuple<string, bool> Verify(object o, PropertyInfo prop)
		{
			bool errorFlag = false;

			if (SimpleCheck.IsNull(o))
			{
				return Tuple.Create(GetErrorInfo(), errorFlag);
			}
			object targetValue = prop.GetValue(o);
			if (SimpleCheck.IsNull(targetValue) || SimpleCheck.IsNullOrEmpty(targetValue.ToString()))
			{
				if (_min<=0)
				{
					errorFlag = true;
				}
				return Tuple.Create(GetErrorInfo(), errorFlag);
			}
			KeyValue<bool, string> result = _checkType.VerifyRange(targetValue, prop, _max, _min);

			if (!SimpleCheck.IsNullOrEmpty(result.Value))
			{
				_errorInfo = result.Value;
			}
			if (result.Key)
			{
				_errorInfo = "验证通过";
			}

			return Tuple.Create(GetErrorInfo(), result.Key);
		}
	}
}
